﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomerCareAutoList.aspx.cs" Inherits="famails.BackEnd.Customer.CustomerCareAutoList" MasterPageFile="~/Master.Master" %>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div style="width: 100%; padding: 10px;">
        <!--start content 01-->
        <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12" style="border: 1px solid #333; margin-bottom: 10px; padding: 0px;">
            <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12" style="padding: 10px 20px; background: #000; color: #fff; font-size: 18px; font-weight: bold; position: relative;">
                Danh sách gửi email chăm sóc khách hàng auto
            </div>

            <asp:Panel Visible="false" ID="pnError" runat="server">
                <div class="error-box round col-md-12 col-xs-12 col-sm-12 col-lg-12" style="padding: 10px;">
                    <asp:Label ID="lblError" runat="server" Text=""></asp:Label>
                </div>
            </asp:Panel>
            <asp:Panel Visible="false" ID="pnSuccess" runat="server">
                <div class="confirmation-box round col-md-12 col-xs-12 col-sm-12 col-lg-12" style="padding: 10px;">
                    <asp:Label ID="lblSuccess" runat="server" Text=""></asp:Label>
                </div>
            </asp:Panel>
            <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12" style="height: auto; padding: 10px;">
                <asp:Panel ID="Panel1" runat="server">
                    <fieldset style="padding: 5px 0px;" id="pnSearch">
                        <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="padding: 5px; padding-left: 0px; margin-bottom: 25px;">
                            <span for="simple-input" style="padding: 5px 10px; border: 1px solid #333; font-weight: bolder; font-size: 18px; text-transform: none"><span style="margin-right: 5px;" class="fa fa-search"></span>Tìm kiếm khách hàng</span>
                        </div>

                        <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="padding: 0px; padding-left: 0px;">
                            <div class="col-md-2 col-lg-2 col-sm-3 col-xs-6" style="padding: 5px; line-height: 30px;">Tiêu đề</div>
                            <div class="col-md-4 col-lg-4 col-sm-3 col-xs-6" style="padding: 5px;">
                                <asp:TextBox runat="server" CssClass="form-control" Style="border-radius: 0px; border: 1px solid #333;" ID="txtSubject"></asp:TextBox>
                            </div>
                            <div class="col-md-2 col-lg-2 col-sm-3 col-xs-6" style="padding: 5px; line-height: 30px;">Nhóm khách hàng</div>
                            <div class="col-md-4 col-lg-4 col-sm-3 col-xs-6" style="padding: 5px;">
                                <asp:DropDownList ID="ddlGroup" runat="server" Style="border-radius: 0px; border: 1px solid #333;" CssClass="form-control"></asp:DropDownList>
                            </div>
                            <div class="col-md-2 col-lg-2 col-sm-3 col-xs-6" style="padding: 5px; line-height: 30px;">Nhóm sản phẩm</div>
                            <div class="col-md-4 col-lg-4 col-sm-3 col-xs-6" style="padding: 5px;">
                                <div id="dropDownButton" class="form-control" style="background: #fff; color: #333; border: 1px solid #333; border-radius: 0px; height: 32px;">
                                    <div id="jqxTree">
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-2 col-lg-2 col-sm-3 col-xs-6" style="padding: 5px; line-height: 30px;">Từ ngày</div>
                            <div class="col-md-4 col-lg-4 col-sm-3 col-xs-6" style="padding: 5px; height: 40px;">
                                <div id="txtFromDate"></div>
                            </div>
                            <div class="col-md-2 col-lg-2 col-sm-3 col-xs-6" style="padding: 5px; line-height: 30px;">Đến ngày</div>
                            <div class="col-md-4 col-lg-4 col-sm-3 col-xs-6" style="padding: 5px;">
                                <div id="txtToDate"></div>
                            </div>
                            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12" style="padding: 0px">
                                <div id="btnSearch" style="float: left; background: #333; color: #fff; text-align: center; padding: 6px 15px; border-radius: 0px; border: 1px solid #fff;" class="btn col-xs-6 col-lg-2 col-sm-3 col-md-2">
                                    <span class="fa fa-desktop" style="margin-right: 10px;"></span>Lọc dữ liệu 
                                </div>
                                <div id="btnExportExcel" style="float: left; background: #333; color: #fff; text-align: center; padding: 6px 15px; border-radius: 0px; border: 1px solid #fff;" class="btn col-xs-6 col-lg-2 col-sm-3 col-md-2">
                                    <span class="fa fa-mail-forward" style="margin-right: 10px;"></span>Xuất dữ liệu 
                                </div>
                                <a href="CustomerCareAuto.aspx" style="float: left; margin-right: 10px; background: #333; color: #fff; text-align: center; padding: 6px 15px; border-radius: 0px; border: 1px solid #fff;" class="btn col-xs-12 col-lg-3 col-sm-6 col-md-4"><span class="fa fa-plus" style="margin-right: 10px;"></span>Chăm sóc khách hàng</a>

                            </div>
                    </fieldset>
                </asp:Panel>

            </div>
            <div class="col-sm-12 col-xs-12 col-md-12 col-lg-12 " style="padding: 0px;">
                <div id='jqxWidget' style="width: 100% !important; font-size: 13px; font-family: Verdana; float: left;">
                    <div id="jqxgrid" style="width: 100% !important;"></div>
                </div>

                <div id="popupWindow">
                    <div>
                        Xóa đợt email
                    </div>
                    <div style="overflow: hidden;">
                        <p>
                            Bạn có chắc là muốn xóa đợt email này không?
                        </p>
                        <button id="del">
                            Yes</button>
                        <button id="cancel">
                            No</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <input type="hidden" id="p_groupid" />
    <script type="text/javascript">
        jQuery(function ($) {

            $("#txtFromDate").jqxDateTimeInput({ width: '120px', height: '25px' });
            $("#txtToDate").jqxDateTimeInput({ width: '120px', height: '25px' });

            $("#btnExportExcel").click(function () {
                $("#jqxgrid").jqxGrid('exportdata', 'xls', 'customerlist');
            });

                var editcellsrenderer = function (row, column, value) {
                    var id = $("#jqxgrid").jqxGrid('getcellvalue', row, "CustomerCareAutoID");
                    //alert(name);
                    return '<a style="margin: 4px;color:#000;" href="CustomerCareAuto.aspx?id=' + id + '">Chỉnh sửa</a>';
                };
            $("#btnSearch").click(function () {
                var url = "CustomerCareAutoList.aspx/Search";
                $("#jqxgrid").jqxGrid('showloadelement');
                var postValue = "{subject:'" + $('#ContentPlaceHolder1_txtSubject').val() + "',c_group:" + $('#ContentPlaceHolder1_ddlGroup').val() + ", p_group:" + $('#p_groupid').val() + ",from:'" + $('#txtFromDate').val() + "',to:'" + $('#txtToDate').val() + "'}";
                //alert(postValue);
                $.ajax({
                    type: "POST",
                    url: url,
                    data: postValue,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var source =
                {
                    datatype: "json",
                    datafields: [
                        { name: 'CustomerCareAutoID', type: 'decimal' },
                        { name: 'Subject', type: 'string' },
                        { name: 'Sender', type: 'string' },
                          { name: 'StartDate', type: 'date' },
                          { name: 'CustomerGroupName', type: 'string' },
                          { name: 'ProductGroupName', type: 'string' }
                    ],
                    id: 'CustomerCareAutoID',
                    localdata: data,
                    pager: function (pagenum, pagesize, oldpagenum) {
                    }
                };
                        var dataAdapter = new $.jqx.dataAdapter(source);
                        $("#jqxgrid").jqxGrid(
                        {
                            width: $('#jqxWidget').width(),
                            source: dataAdapter,
                            selectionmode: 'multiplerowsextended',
                            sortable: true,
                            pageable: true,
                            autoheight: true,
                            columnsresize: true,
                            columns: [
                              { text: 'Tiêu đề', datafield: 'Subject' },
                              { text: 'Email gửi', datafield: 'Sender' },
                              { text: 'Ngày bắt đầu gửi', datafield: 'StartDate', width: 100, cellsformat: 'dd/MM/yyyy', cellsalign: 'center' },
                              { text: 'Nhóm khách hàng', datafield: 'CustomerGroupName', width: 200 },
                              { text: 'Nhóm sản phẩm', datafield: 'ProductGroupName', width: 200 },
                              {
                                  text: 'Xem kết quả', datafield: 'Edit', columntype: 'button', width: 120, cellsrenderer: function () {
                                      return "Xem kết quả";
                                      //return '<input type="image" style="margin-left: 5px;" height="30" width="30" src="../resource/images/delete-icon.png"/>';
                                  }, buttonclick: function (row) {
                                      editrowindex = row;
                                      var id = $("#jqxgrid").jqxGrid('getcellvalue', row, "CustomerCareAutoID");
                                      window.location = 'CustomerCareAutoResult.aspx?id=' + id;
                                  }
                              },
                                          { text: 'Chỉnh sửa', datafield: 'EventId', width: 100, cellsrenderer: editcellsrenderer },
                               {
                                   text: 'Xóa', datafield: 'Delete', columntype: 'button', width: 50, cellsrenderer: function () {
                                       return "Xóa";
                                   }, buttonclick: function (row) {
                                       // open the popup window when the user clicks a button.
                                       id = $("#jqxgrid").jqxGrid('getrowid', row);
                                       var offset = $("#jqxgrid").offset();
                                       $("#popupWindow").jqxWindow({ position: { x: parseInt(offset.left) + 60, y: parseInt(offset.top) + 60 } });
                                       // show the popup window.
                                       $("#popupWindow").jqxWindow('show');
                                   }
                               }]
                        });

                    }
                });
            });
            $("#popupWindow").jqxWindow({ width: 250, resizable: false, theme: theme, isModal: true, autoOpen: false, cancelButton: $("#Cancel"), modalOpacity: 0.01 });
            $("#del").jqxButton({ theme: theme });
            $("#cancel").jqxButton({ theme: theme });
            $("#del").click(function () {
                $.ajax({
                    type: "POST",
                    url: "CustomerCareAutoList.aspx/Delete",
                    data: "{id:" + id + "}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $('#jqxgrid').jqxGrid('deleterow', id);
                        $("#popupWindow").jqxWindow('hide');
                    },
                    error: function (request, status, error) {
                        alert('Đợt gửi email này đã được gửi, bạn không thể xóa!');
                        $("#popupWindow").jqxWindow('hide');
                    }
                });
            });
            $("#cancel").click(function () {
                $("#popupWindow").jqxWindow('hide');
            });

            loadProductGroup();
            $('#jqxTree').trigger('select');
            $('#btnSearch').trigger('click');
            function loadProductGroup() {
                $.ajax({
                    type: "POST",
                    url: "CustomerCareAutoList.aspx/LoadTree",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data.d != undefined) {
                            var html = "<ul>";
                            $.each(data.d, function (i, item) {
                                if (item.ProductGroupName != undefined) {
                                    if (i == 0) html += "<li  item-selected='true' item-expanded='true' item-value='" + item.ProductGroupID + "'>" + item.ProductGroupName;
                                    else html += "<li item-expanded='true' item-value='" + item.ProductGroupID + "'>" + item.ProductGroupName;
                                    //alert(item.children.count);
                                    if (item.children != "") {
                                        html += "<ul>";
                                        html += getChild(item.children);
                                        html += "</ul>";
                                    }
                                    html += "</li>";
                                }
                            });
                            html += "</ul>";
                        }
                        $("#jqxTree").append(html);
                        $("#dropDownButton").jqxDropDownButton({ width: 150, height: 20 });
                        $('#jqxTree').on('select', function (event) {
                            var args = event.args;
                            var item = $('#jqxTree').jqxTree('getItem', args.element);
                            $("#p_groupid").val(item.value);
                            var dropDownContent = '<div style="position: relative; margin-left: 3px; margin-top: 5px;">' + item.label + '</div>';
                            $("#dropDownButton").jqxDropDownButton('setContent', dropDownContent);
                            //loadProduct();
                        });
                        $("#jqxTree").jqxTree({ height: 220, width: 150 });
                    }
                });
            }
            function getChild(child) {
                var html = "";
                if (child != "") {
                    $.each(child, function (i, item) {
                        html += "<li item-expanded='true' item-value='" + item.ProductGroupID + "'>" + item.ProductGroupName;
                        if (item.children != "") {
                            html += "<ul>";
                            html += getChild(item.children);
                            html += "</ul>";
                        }
                        html += "</li>";
                    });
                }
                return html;
            }
        });
    </script>
</asp:Content>
